<?php


namespace app\cms\model\admin;


use app\cms\community\lib\QueryModel;
use iflow\Response;
use think\model\relation\HasManyThrough;
use think\model\relation\HasOne;

class adminUserNavModel extends QueryModel
{

    protected $table = 'admin_user_nav_table';

    public function hasAdminNavGroup(): HasOne
    {
        return $this->hasOne(adminNavGroupModel::class, 'nav_group_id', 'nav_group_id');
    }

    // 远程一对一连表查询
    public function hasAdminUserNav(): HasManyThrough
    {
        return $this->hasManyThrough(
            adminNavGroupModel::class,
            adminNavModel::class,
            'nav_group_id',
            'nav_group_id'
        );
    }


    /**
     * 通过 用户id 以及 导航类目 获取导航栏
     * @param int $uid
     * @param int $nav_group_id
     * @param bool $needPaginate
     * @return Response|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNavListByUidAndNavGroupId(int $uid, int $nav_group_id, bool $needPaginate = false): Response|array
    {
         $query = $this->hasAdminUserNav() -> where([
            'admin_user_table.uid' => $uid,
            'admin_nav_table.nav_group_id' => $nav_group_id,
            'admin_nav_table.nav_parent_id' => '',
        ]);

        if ($needPaginate) return $this->paginateQuery($query);
        return $query -> select() ?-> toArray();
    }

    /**
     * 获取导航栏子类目
     * @param int $uid
     * @param int $nav_group_id
     * @param string $nav_parent_id
     * @param bool $needPaginate
     * @return array|Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChildRenNavListByUidAndNavGroupId(int $uid, int $nav_group_id, string $nav_parent_id, bool $needPaginate = false): Response|array
    {
        $query = $this->hasAdminUserNav() -> where([
            'admin_user_table.uid' => $uid,
            'admin_nav_table.nav_group_id' => $nav_group_id,
            'admin_nav_table.nav_parent_id' => $nav_parent_id,
        ]);

        if ($needPaginate) return $this->paginateQuery($query);
        return $query -> select() ?-> toArray();
    }

    /**
     * 设置当前左侧导航
     * @param int $uid
     * @param int $nav_group_id
     * @param int $nav_id
     * @return Response
     */
    public function setAdminUserNav(int $uid, int $nav_group_id, int $nav_id): Response
    {
        $menu = $this->where([
            'uid' => $uid,
            'nav_group_id' => $nav_group_id,
            'nav_id' => $nav_id
        ]) -> findOrEmpty();

        if ($menu -> isExists()) return message() -> update_failed('当前用户已绑定该菜单');

        return $this->createData([
            'uid' => $uid,
            'nav_group_id' => $nav_group_id,
            'nav_id' => $nav_id
        ]);
    }

    /**
     * 删除用户绑定左侧导航
     * @param int $uid
     * @param int $nav_group_id
     * @param int $nav_id
     * @return Response
     */
    public function deleteAdminUserNav(int $uid, int $nav_group_id, int $nav_id): Response
    {
        return $this->deleteData([
            'uid' => $uid,
            'nav_group_id' => $nav_group_id,
            'nav_id' => $nav_id,
        ]);
    }

}